//
//  WJPDFOperationViewController.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/28.
//

import UIKit
import SnapKit

enum OperationType {
    case rename
    case share
    case print
    case delete
}

protocol WJPDFOperationViewControllerDelegate: AnyObject {
    func operationPDF(_ controller: WJPDFOperationViewController, operationType: OperationType, listModel: WJPDFListModel)
}
  
class WJPDFOperationViewController: UIViewController {
    
    weak var delegate: WJPDFOperationViewControllerDelegate? = nil
    var listModel: WJPDFListModel? = nil
  
    // 懒加载背景模糊效果
    private lazy var blurEffectView: UIView = {
        // 1. 创建基础的毛玻璃效果视图
        let blurEffect = UIBlurEffect(style: .light) // 也可以尝试 .dark 或 .extraLight
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
//        blurEffectView.frame = self.bounds // 设置frame或使用Auto Layout
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        
        // 2. 创建颜色覆盖层
        let colorTintView = UIView(frame: blurEffectView.bounds)
        colorTintView.backgroundColor = UIColor.white.withAlphaComponent(0.6) // 调整颜色和透明度
        colorTintView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        
        // 3. 将颜色层添加到毛玻璃视图的contentView
        blurEffectView.contentView.addSubview(colorTintView)
        
        return blurEffectView
    }()
  
    // 操作视图
    private lazy var operationView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#C8C9CA")
        view.layer.cornerRadius = 10
        view.layer.borderColor = UIColor(hex: "#FFFFFF").cgColor
        view.layer.borderWidth  = 0.5
        return view
    }()
  
    // 文档视图
    private lazy var documentView: WJOperationDocumentView = {
        let view = WJOperationDocumentView()
        return view
    }()
  
    // 操作按钮的标题和图片
    private let operations: [(title: String, imageName: String)] = [
        ("Rename", "Icon_line_rename"),
        ("Share", "Icon_line_share"),
        ("Print", "Icon_print"),
        ("Delete", "Icon_line_delete")
    ]
  
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
  
    private func setupUI() {
        view.backgroundColor = .clear
        view.addSubview(blurEffectView)
        blurEffectView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
  
        // 添加文档视图
        documentView.listModel = listModel
        view.addSubview(documentView)
        documentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(28)
            make.height.equalTo(72)
        }
  
        // 添加操作视图
        view.addSubview(operationView)
        operationView.snp.makeConstraints { make in
            make.width.equalTo(220)
            make.height.equalTo(operations.count * 51 - 1)
            make.top.equalTo(documentView.snp.bottom).offset(5)
            make.trailing.equalTo(documentView)
        }
  
        setupOperationButtons()
    }
  
    private func setupOperationButtons() {
        for (index, operation) in operations.enumerated() {
            let button = WJCustomOperationButton(title: operation.title, imageName: operation.imageName)
            button.tag = index
            button.addTarget(self, action: #selector(operationButtonTapped(_:)), for: .touchUpInside)
            button.backgroundColor = UIColor(hex: "#DDDEDF")
            operationView.addSubview(button)
            button.snp.makeConstraints { make in
                make.left.equalToSuperview()
                make.right.equalToSuperview()
                make.height.equalTo(50)
                make.top.equalToSuperview().offset(index * 51)
            }
        }
    }
  
    @objc private func operationButtonTapped(_ sender: UIButton) {
        var operationType: OperationType = .rename
        switch sender.tag {
        case 0:
            operationType = .rename
        case 1:
            operationType = .share
        case 2:
            operationType = .print
        case 3:
            operationType = .delete
        default:
            break
        }
        
        dismiss(animated: true) { [weak self] in
            guard let self = self else { return }
            if let listModel = documentView.listModel {
                self.delegate?.operationPDF(self, operationType: operationType, listModel: listModel)
            }
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        dismiss(animated: true)
    }
}
  
